package com.kl.forum.service;

import com.github.pagehelper.PageInfo;
import com.kl.forum.entity.*;
import com.kl.forum.enums.PushType;
import com.kl.forum.enums.ResultCode;
import com.kl.forum.util.DeleteFileUtil;
import com.kl.forum.util.OssUtil;
import com.kl.forum.util.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.kl.forum.util.ListUtil.randomTopic;

@Service
@Transactional
public class CircleService {
    @Autowired
    private  UserCoverUserService userCoverUserService;
    @Autowired
    private  ReportService reportService;
    @Autowired
    private  UserCircleService userCircleService;
    @Autowired
    private PushService pushService;
    @Autowired
    private  MemberService memberService;
    @Autowired
    private  CircleDicService circleDicService;
    @Autowired
    private  CircleReplyService circleReplyService;
    @Autowired
    private  CircleArticleLightService circleArticleLightService;

    @Autowired
    private  CircleCommentService circleCommentService;

    @Autowired
    private CircleArticleService circleArticleService;

    @Autowired
    private  CirclePicService circlePicService;

    @Autowired
    private  CircleCollectService circleCollectService;

    @Autowired
    private   CircleCommentLightService circleCommentLightService;
    @Autowired
    private  CircleReplyLightService circleReplyLightService;
    @Autowired
    private  AdListService adListService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public  static  PageInfo  adAndArt(List<Map> list,PageInfo data){
        List listDate=   data.getList();
        if (listDate.size()>13){
            if (list!=null){
                List<Map> data1= randomTopic(list,3);
                if (data1!=null) {
                    listDate.add(4, data1.get(0));
                    listDate.add(8, data1.get(1));
                    listDate.add(12, data1.get(2));
                    data.setList(listDate);
                }
            }
        }
        return  data;
    }

    /**
     * 最后一条拼接广告
     * @param list
     * @param data
     * @return
     */
    public  static  PageInfo  adAndArt6(List<Map> list,PageInfo data){
        List listDate=   data.getList();
       if (listDate!=null&&listDate.size()>0){
           List<Map> data1= randomTopic(list,1);
           if (data1!=null){
               listDate.add(data1.get(0));
               data.setList(listDate);
           }
       }
        return  data;
    }
    /**
     *精选内容
     * @return
     */
    public ResultInfo findEssenceCircle(Integer userId,Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data=circleArticleService.findEssenceCircle(userId,pageNum,pageSize);
            List<Map> list=adListService.findAdvertisement();
            re.setData(adAndArt6(list,data));
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 发表帖子
     * @param userId
     * @param circleId
     * @param content
     * @return
     */
    public ResultInfo insertCircleArticle(Integer userId, Integer circleId, String content, List<MultipartFile> files, String type )    {
        ResultInfo re=new ResultInfo() ;
        Integer circleArticleId;
        String video="";
        String videoScreenshots="";
        try {
            if(files!=null){
                if (type.equals("video")){
                    String filename = files.get(0).getOriginalFilename();
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(files.get(0).getBytes());
                    os.close();
                    files.get(0).transferTo(newFile);
                    video=  OssUtil.up(newFile) ;
                    // 删除上传的文件
                    deleteFile(filename);
//                    缩略图
                    String filenameVideoPic = files.get(1).getOriginalFilename();
                    File newFileV = new File(filenameVideoPic);
                    FileOutputStream osV = new FileOutputStream(newFileV);
                    osV.write(files.get(1).getBytes());
                    osV.close();
                    files.get(1).transferTo(newFileV);
                    videoScreenshots=  OssUtil.up(newFileV) ;
                    // 删除上传的文件
                    deleteFile(filenameVideoPic);
                }
                circleArticleId= circleArticleService.insertCircleArticle(userId,circleId,content,video,videoScreenshots);
                if (type.equals("pic")){
                    for (int i=0;i<files.size();i++){
                        String filename = files.get(i).getOriginalFilename();
                        File newFile = new File(filename);
                        FileOutputStream os = new FileOutputStream(newFile);
                        os.write(files.get(i).getBytes());
                        os.close();
                        files.get(i).transferTo(newFile);
                        String  picPath=  OssUtil.up(newFile) ;
                        // 删除上传的文件
                        deleteFile(filename);
                        circlePicService.insert(circleArticleId,picPath);
                    }
                }
            }else {
                circleArticleId= circleArticleService.insertCircleArticle(userId,circleId,content,video,videoScreenshots);
            }
            stringRedisTemplate.opsForHash().put("circleArticleViewNumCount",String.valueOf(circleId),"0");
            stringRedisTemplate.opsForHash().put("circleArticleLightNumCount",String.valueOf(circleId),"0");
             re.setMessage(ResultCode.SUCCESS.getMsg());
             re.setCode(ResultCode.SUCCESS.getCode());

            //redis 计数器
//            Map map=new HashMap();
//            map.put("light_num","0");//点赞次数
//            map.put("comment_num","0");//评论
//            map.put("collect_num","0");//收藏
//            map.put("view_num","0"); //浏览次数
//            stringRedisTemplate.opsForHash().putAll("counter:circle_article:"+circleArticleId,map);
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    //删除临时文件
    public   static  void   deleteFile(String fileName){
        File file=new File("");
        String sv = file.getAbsolutePath();
        DeleteFileUtil.delete(sv + File.separator + fileName);
    }

    /**
     * 删除帖子 逻辑删除
     * @return
     */
    public ResultInfo delCircleArticle(Integer circleArticleId) {
        ResultInfo re=new ResultInfo();
        try {
            circleArticleService.delCircleArticle(circleArticleId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    /**
     * 评论列表
     * @return
     */
    public ResultInfo findCircleComment(Integer userId, Integer circleArticleId,Integer pageNum,Integer pageSize,String sort) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data= circleCommentService.findCircleComment(userId,circleArticleId,pageNum,pageSize,sort);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 发表评论
     * @param userId
     * @param circleArticleId
     * @param content
     * @return
     */
    public ResultInfo insertCircleComment(Integer userId, Integer circleArticleId, String content) {
        ResultInfo re=new ResultInfo();
        Date date= new Date();
        try {
            int id=circleCommentService.insertCircleComment(userId,circleArticleId,content,date);
            CircleArticle article=circleArticleService.findOneCircleArt(circleArticleId);
//            article.setCommentNum(article.getCommentNum()+1);
            article.setReplyTime(date);
            circleArticleService.update(article);
            pushService.pushArticleMessage(article.getUserId(),PushType.Comment.getCode());
            pushService.addPuhsNum(article.getUserId(),PushType.Comment.getCode());
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 点赞文章
     * @param articleId
     * @param userId
     * @return
     */
    public ResultInfo lightCircleArticle(Integer articleId, Integer userId) {
        ResultInfo re=new ResultInfo();
        CircleLight light= circleArticleLightService.findOneCircleLight(articleId,userId);
       try {
           if (light!=null){
               if (light.getStatus()==1){
                   re.setData("您已经点过赞");
                   return   re;
               }
               light.setStatus(1);
               circleArticleLightService.update(light);
               re.setMessage(ResultCode.SUCCESS.getMsg());
               re.setCode(ResultCode.SUCCESS.getCode());
               return   re;
           }
           CircleLight data=new CircleLight();
           data.setStatus(1);
           data.setUserId(userId);
           data.setCircleId(articleId);
           data.setCreateTime(new Date());
           circleArticleLightService.insert(data);
           Boolean b= stringRedisTemplate.opsForHash().hasKey("circleArticleLightNumCount",articleId.toString());
           if (!b){
               stringRedisTemplate.opsForHash().put("circleArticleLightNumCount",articleId.toString(),"1");
           }else {
               stringRedisTemplate.opsForHash().increment("circleArticleLightNumCount",articleId.toString(),1);
           }
           CircleArticle article=circleArticleService.findOneCircleArt(articleId);
//           article.setLightNum(article.getLightNum()+1);
//           circleArticleService.update(article);
           pushService.pushArticleMessage(article.getUserId(),PushType.Light.getCode());
           pushService.addPuhsNum(article.getUserId(),PushType.Light.getCode());
           re.setMessage(ResultCode.SUCCESS.getMsg());
           re.setCode(ResultCode.SUCCESS.getCode());
//           stringRedisTemplate.opsForHash().increment("counter:circle_article:"+articleId,"light_num",1);
           return  re;
       }catch (Exception e){
           e.printStackTrace();
       }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }



    /**
     * 点灭文章
     * @param articleId
     * @param userId
     * @return
     */
    public ResultInfo darkCircleArticle(Integer articleId, Integer userId) {
        ResultInfo re=new ResultInfo();
        CircleLight light= circleArticleLightService.findOneCircleLight(articleId,userId);
        try {
            light.setStatus(0);
            circleArticleLightService.update(light);
            CircleArticle article=new CircleArticle();
            article.setLightNum(article.getLightNum()-1);
            article.setId(articleId);
            circleArticleService.update(article);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    /**
     * 收藏文章
     * @param articleId
     * @param userId
     * @return
     */
    public ResultInfo insertCircleCollect(Integer articleId, Integer userId) {
         ResultInfo re=new ResultInfo();
         try {
             CircleCollect circleCollect=circleCollectService.findOneCircleCollect(articleId,userId);
             if (circleCollect==null){
                 circleCollectService.insert(articleId,userId);
                 re.setMessage(ResultCode.SUCCESS.getMsg());
                 re.setCode(ResultCode.SUCCESS.getCode());
//                 stringRedisTemplate.opsForHash().increment("counter:circle_article:"+articleId,"collect_num",1);
                 return  re;
             }else{
                 if (circleCollect.getStatus()==1){
                     re.setData("已经收藏过了");

                 }
                 circleCollect.setStatus(1);
                 circleCollectService.update(circleCollect);
                 re.setMessage(ResultCode.SUCCESS.getMsg());
                 re.setCode(ResultCode.SUCCESS.getCode());
//                 stringRedisTemplate.opsForHash().increment("counter:circle_article:"+articleId,"collect_num",1);
                 return  re;
             }
         }catch (Exception e){
             e.printStackTrace();
         }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 取消收藏文章
     * @param articleId
     * @param userId
     * @return
     */
    public ResultInfo updateCircleCollect(Integer articleId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            CircleCollect circleCollect=circleCollectService.findOneCircleCollect(articleId,userId);
            circleCollect.setStatus(0);
            circleCollectService.update(circleCollect);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
//            stringRedisTemplate.opsForHash().increment("counter:circle_article:"+articleId,"collect_num",-1);
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 点赞评论
     * @return
     */
    public ResultInfo lightCircleComment(Integer commentId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
//            因为要排序所以没有存到redis
//            Boolean b= stringRedisTemplate.opsForHash().hasKey("circleArticleCommentLightNumCount",String.valueOf(commentId));
//            if (!b){
//                stringRedisTemplate.opsForHash().put("circleArticleCommentLightNumCount",String.valueOf(commentId),"1");
//            }else {
//                stringRedisTemplate.opsForHash().increment("circleArticleCommentLightNumCount",String.valueOf(commentId),1);
//            }
            CircleComment comment= circleCommentService.findOneCircleCommentBykey(commentId);
            comment.setLightNum(comment.getLightNum()+1);
            circleCommentService.update(comment);
//            stringRedisTemplate.opsForHash().increment("counter:circle_article_comment:"+commentId,"light_num",1);

            CircleCommentLight light= circleCommentLightService.findOneCircleCommentLight(commentId,userId);
            if(light!=null){
                if (light.getStatus()==1){
                    re.setData("您已经点过赞");
                }
                light.setStatus(1);
                circleCommentLightService.update(light);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return   re;
            }else {
                circleCommentLightService.insert(commentId,userId);
                pushService.pushArticleMessage(comment.getUserId(),PushType.Light.getCode());
                pushService.addPuhsNum(comment.getUserId(),PushType.Light.getCode());
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 取消点赞评论
     * @return
     */
    public ResultInfo darkCircleComment(Integer commentId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            CircleComment comment= circleCommentService.findOneCircleCommentBykey(commentId);
            comment.setLightNum(comment.getLightNum()-1);
            circleCommentService.update(comment);
            CircleCommentLight light= circleCommentLightService.findOneCircleCommentLight(commentId,userId);
            light.setStatus(0);
            circleCommentLightService.insert(commentId,userId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    public ResultInfo delCircleComment(Integer commentId) {
        ResultInfo re= new ResultInfo();
        try {
            circleCommentService.delCircleComment(commentId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    /**
     * 发表回复
     * @return
     */
    public ResultInfo insertCircleCommentReply(Integer commentId,Integer userId,String userName,String content,Integer articleId,String commentName,Integer commentNameId) {
        ResultInfo re=new ResultInfo();
        Date date= new Date();
        try {
            CircleArticle circleArticle= circleArticleService.findOneCircleArt(articleId);
//            circleArticle.setCommentNum(circleArticle.getCommentNum()+1);
            circleArticle.setReplyTime(date);
            circleArticleService.update(circleArticle);
            CircleComment circleComment= circleCommentService.findOneCircleCommentBykey(commentId);
//            circleComment.setReply(circleComment.getReply()+1);
//            circleCommentService.update(circleComment);
            CircleReply circleReply= circleReplyService.insert(commentId,userId,content,date,userName,commentName,commentNameId,articleId);
            re.setData(circleReply);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
//            pushService.pushArticleMessage(circleArticle.getUserId(),PushType.Reply.getCode());
//            pushService.addPuhsNum(circleArticle.getUserId(),PushType.Reply.getCode());
            if (circleComment!=null){
                pushService.pushArticleMessage(circleComment.getUserId(),PushType.Reply.getCode());
                pushService.addPuhsNum(circleComment.getUserId(),PushType.Reply.getCode());
            }
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findCircleCommentReply(Integer commentId, Integer userId,Integer pageNum,Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data=circleReplyService.findCircleCommentReply(commentId,userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 删除回复
     * @param
     * @param
     * @return
     */
    public ResultInfo delCircleCommentReply(Integer replyId) {
        ResultInfo re=new ResultInfo();
        try {
            circleReplyService.delCircleCommentReply(replyId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;

        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    /**
     * 点赞回复
     * @param
     * @param
     * @return
     */
    public ResultInfo lightCircleCommentReply(Integer replyId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            CircleReplyLight light=circleReplyLightService.findCircleReplyLight(replyId,userId);
            CircleReply reply= circleReplyService.findOneCircleReply(replyId);
            reply.setLightNum(reply.getLightNum()+1);
            circleReplyService.update(reply);
//            stringRedisTemplate.opsForHash().increment("counter:circle_article_comment_reply:"+replyId,"light_num",1);
            if (light!=null){
                light.setStatus(1);
                circleReplyLightService.update(light);
            }else {
                circleReplyLightService.insert(replyId,userId);
            }
            pushService.pushArticleMessage(userId,PushType.Light.getCode());
            pushService.addPuhsNum(userId,PushType.Light.getCode());
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    /**
     * 点灭回复
     * @param
     * @param
     * @return
     */
    public ResultInfo darkCircleCommentReply(Integer replyId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            CircleReplyLight light=circleReplyLightService.findCircleReplyLight(replyId,userId);
            CircleReply reply= circleReplyService.findOneCircleReply(replyId);
            reply.setLightNum(reply.getLightNum()-1);
            circleReplyService.update(reply);
            light.setStatus(0);
            circleReplyLightService.update(light);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 所有圈子
     * @return
     */
    public ResultInfo findCircle() {
        ResultInfo re= new ResultInfo();
        try {
            List data=circleDicService.findCircle();
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 圈子详情
     * @param circleId
     * @param userId
     * @return
     */
    public ResultInfo findCircleOne(Integer circleId, Integer userId) {
        ResultInfo re= new ResultInfo();
        try {
            CircleDic data =circleDicService.findCircleOne(circleId,userId);

            if (userId!=null){
                data.setViewNum(data.getViewNum()+1);
                circleDicService.update(data);
            }
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 置顶帖子
     * @param circleId
     * @return
     */
    public ResultInfo findCircleArticleTop(Integer circleId) {
        ResultInfo re= new ResultInfo();
        try {
            List<Map> data= circleArticleService.findCircleArticleTop(circleId);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 帖子
     * @param circleId
     * @return
     */
    public ResultInfo findCircleArticleBySort(Integer userId,Integer circleId,Integer pageNum,Integer pageSize,String sort) {
        ResultInfo re= new ResultInfo();
        try {
            PageInfo data= circleArticleService.findCircleArticleBySort(userId,circleId,pageNum,pageSize,sort);
            List<Map> list=adListService.findAdvertisement();
            re.setData(adAndArt6(list,data));
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }



    /**
     * 更多关注
     * @return
     */
    public ResultInfo findUserByRecommend(Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            List<Member> data= memberService.findUserByRecommend(userId);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 我的点赞
     * @param userId
     * @return
     */
    public ResultInfo findMyLight(Integer userId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data= circleArticleService.findMyLight(userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    public ResultInfo findMyComment(Integer userId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data= circleArticleService.findMyComment(userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findCircleArticleDetail(Integer userId, Integer id) {
        ResultInfo re=new ResultInfo();
        try {
            CircleArticle data= circleArticleService.findCircleArticleDetail(userId,id);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo insertUserCircle(Integer userId, Integer circleId) {
        ResultInfo re =new ResultInfo();
        try {

            UserCircle userCircle= userCircleService.findOneUserCircle(userId,circleId);
            if (userCircle==null){
                userCircleService.insertUserCircle(userId,circleId);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }else {
                if(userCircle.getStatus()==1) {
                    re.setData("已经关注过了");
                }
                userCircleService.updateUserCircle(userId,circleId,1);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo updateUserCircle(Integer userId, Integer circleId) {
        ResultInfo re =new ResultInfo();
        try {
            userCircleService.updateUserCircle(userId,circleId,0);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findMyCircle(Integer userId,Integer pageNum,Integer pageSize) {
        ResultInfo re= new ResultInfo();
        try {
            PageInfo data=  circleDicService.findMyCircle(userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo report(Integer userId, Integer accusedUserId,Integer reportType, Integer articleId,String content) {
        ResultInfo re=new ResultInfo();
        try {
            reportService.insert(userId,accusedUserId,reportType,articleId,content);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    public ResultInfo findArticleCommentReplyList(Integer userId, Integer commentId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            CircleComment comment=   circleReplyService.findArticleCommentReplyList(userId,commentId,pageNum,pageSize);
            re.setData(comment);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo insertCoverUser(Integer userId, Integer coverUserId) {
        ResultInfo re=new ResultInfo();
        try {
            UserCoverUser userCoverUser= userCoverUserService.findOneUserCoverUser(userId,coverUserId);
            if (userCoverUser!=null){
                if (userCoverUser.getStatus()==1) {
                    re.setData("已经关注过了");
                }
                userCoverUserService.updateCoverUser(userId,coverUserId,1);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }else{
                userCoverUserService.insertCoverUser(userId,coverUserId);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    public ResultInfo updateCoverUser(Integer userId, Integer coverUserId) {
        ResultInfo re=new ResultInfo();
        try {
            userCoverUserService.updateCoverUser(userId,coverUserId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }



}
